package com.woniuxy.creditsystem.loanmag.timer;

import com.woniuxy.creditsystem.common.bean.Detail;
import com.woniuxy.creditsystem.common.bean.OrderInfo;
import com.woniuxy.creditsystem.common.bean.ReturnPlanInfo;
import com.woniuxy.creditsystem.loanmag.service.LoanService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

//逾期通知定时器
@Component
public class DueTimer {
    @Resource
    private LoanService loanService;
    @Resource
    private JavaMailSender mailSender;
    @Scheduled(cron = "0 40 21 * * *")  //秒 分 时 月份中的日期 月份 星期中的日期 年份
    //每天晚上0点查询、计算、判断、更新，还款计划表、订单表的状态
    public void doDue(){
        //查询还款计划表所有信息
        List<ReturnPlanInfo> allPlans = loanService.findAllPlans();
        System.out.println(allPlans);
        //遍历
        for (int i = 0; i < allPlans.size(); i++) {
            //如果应当还款时间小于now,则判断是否逾期
            System.out.println(allPlans.get(i).getBillTime().getTime());
            if(allPlans.get(i).getBillTime().getTime() < new Date().getTime()){
                System.out.println("进入第一层if判断逾期");
                //没有实际还款时间视为逾期
                if (allPlans.get(i).getAlsoTime()==null){
                    System.out.println("进入第二层if视为逾期");
                    //处理逾期
                    //一、修改该条还款计划
                    ReturnPlanInfo planInfo = new ReturnPlanInfo();
                        //1.设置罚息
                            //得到订单详情
                            Detail detail = loanService.findByorderid(allPlans.get(i).getOrderNum());
                            System.out.println("订单详情"+detail);
                            //得到订单的罚息日利率
                            BigDecimal penaltyInterestRate = detail.getPenaltyInterestRate();
                            System.out.println("日率="+penaltyInterestRate);
                            //计算逾期罚息 罚息=逾期天数*(罚息日率*本金)
                                //得到逾期天数
                                int day = (int)Math.ceil((new Date().getTime() - allPlans.get(i).getBillTime().getTime())*1.0 / ((1000 * 60 * 60 * 24)*1.0));
                                //得到本金
                                BigDecimal money = detail.getMoney();
                                //计算罚息
                                BigDecimal penaltyInteres = penaltyInterestRate.multiply(new BigDecimal(day)).multiply(money);
                            //设置罚息
                            planInfo.setPenaltyInteres(penaltyInteres);
                                System.out.println("罚息="+penaltyInteres);
                        //2.设置待还金额
                            //待还金额等于当期本金+利息+罚息
                            //得到当期本金
                            BigDecimal principal = allPlans.get(i).getPrincipal();
                            //得到当前利息
                            BigDecimal interest = allPlans.get(i).getInterest();
                            //计算本期待还金额
                            BigDecimal waitmonye = principal.add(interest).add(penaltyInteres);
                            //设置待还金额
                            planInfo.setWaitMoney(waitmonye);
                            System.out.println("待还金额="+waitmonye);
                        //3.设置订单状态
                            planInfo.setAlsoStatus("逾期中");
                        //4.设置逾期天数
                            planInfo.setOverdueDays(day);
                            System.out.println("逾期天数:"+day);
                        //5.设置期数
                            planInfo.setDataNum(allPlans.get(i).getDataNum());
                        //6.设置订单编号
                            planInfo.setOrderNum(allPlans.get(i).getOrderNum());
                        //7.修改数据库还款计划的数据
                            loanService.setPlan(planInfo);
                    //二、修改对应的订单数据
                        //1.查询最大逾期天数
                        int maxday = loanService.getMaxdaysbyOid(allPlans.get(i).getOrderNum());
                        System.out.println("maxday="+maxday);
                        //2.设置逾期天数与状态与订单号
                        OrderInfo orderInfo = new OrderInfo();
                        orderInfo.setOverduedays(maxday); //逾期天数
                        orderInfo.setStatus("逾期中");  //状态
                        orderInfo.setNum(allPlans.get(i).getOrderNum()); //订单号
                        //存入数据库
                        System.out.println("orderinfo="+orderInfo);
                        loanService.setOrder(orderInfo);
                    //三、javamail
                        //设置用户的email
                        String email = detail.getEmail();
                        SimpleMailMessage message = new SimpleMailMessage();
                        message.setFrom("1991865663@qq.com");
                        message.setTo("1991865663@qq.com");
                        message.setSubject("催账信息!");
                        String text=null;

                        String demo=detail.getName()+"先生!你的账单已逾期"+maxday+"天;本期应还:"+principal+"元！"
                                +"产生利息:"+interest+"元！"+"产生滞纳金:"+penaltyInteres+"元!共计待还:"+waitmonye+"元！建议你尽快还款以免影响信用;点击此处还款https://www.woniuxy.com/";

                        if (maxday>0&&maxday<=3){
                            text="温馨提示!"+demo;
                        }else if(maxday>3&&maxday<=7){
                            text="警告!"+demo;
                        }else if(maxday>7&&maxday<=30){
                            text="警告!"+demo;
                        }else{
                            text="已联系打手!";
                        }

                        message.setText(text);
                        mailSender.send(message);
                }
            }
        }
    }
}
